/*
 * Copyright 2025 NXP
 * NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
 * accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
 * activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
 * comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
 * terms, then you may not retain, install, activate or otherwise use the software.
 */

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"
#include "my_app.h"
#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif

uint8_t wifi_switch_flag = 0; // wifi 开关
bool SmartConfig_flag = 0;
bool search_wifi_flag = 0; // 搜索wifi标志位
uint16_t time_year = 2025;
uint8_t time_month = 1;
uint8_t time_day = 1;
uint8_t time_hour = 21;
uint8_t time_min_10 = 5;
uint8_t time_min_01 = 55;

#include "lv_dclock.h"
#include "lv_radiobtn.h"

uint8_t SetDateTimeFlag = 0;

static void screen_home_imgbtn_set_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_set, guider_ui.screen_set_del, &guider_ui.screen_home_del, setup_scr_screen_set, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_home(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_home_imgbtn_set, screen_home_imgbtn_set_event_handler, LV_EVENT_ALL, ui);
}

static void screen_set_list_set_item0_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_set_time, guider_ui.screen_set_time_del, &guider_ui.screen_set_del, setup_scr_screen_set_time, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, false);
        break;
    }
    default:
        break;
    }
}

static void screen_set_list_set_item1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_set_wifi, guider_ui.screen_set_wifi_del, &guider_ui.screen_set_del, setup_scr_screen_set_wifi, LV_SCR_LOAD_ANIM_OVER_LEFT, 200, 200, false, false);
        break;
    }
    default:
        break;
    }
}

static void screen_set_imgbtn_back_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_home, guider_ui.screen_home_del, &guider_ui.screen_set_del, setup_scr_screen_home, LV_SCR_LOAD_ANIM_NONE, 200, 200, false, false);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_set(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_set_list_set_item0, screen_set_list_set_item0_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_list_set_item1, screen_set_list_set_item1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_imgbtn_back, screen_set_imgbtn_back_event_handler, LV_EVENT_ALL, ui);
}

static void screen_home_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_SCREEN_LOADED:
    {

        break;
    }
    default:
        break;
    }
}
static void screen_set_wifi_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_SCREEN_LOADED:
    {
        if (wifi_switch_flag == 0) // 设置wifi开关按钮的状态
        {
            lv_obj_clear_state(guider_ui.screen_set_wifi_sw_WifiSwich, LV_STATE_CHECKED);
        }
        else
        {
            lv_obj_add_state(guider_ui.screen_set_wifi_sw_WifiSwich, LV_STATE_CHECKED);
        }
        lv_obj_add_flag(guider_ui.screen_set_wifi_cont_WifiiPassWord, LV_OBJ_FLAG_HIDDEN);

        search_wifi_flag = 1; // 加载页面时，开启查询wifi

        break;
    }
    default:
        break;
    }
}

static void screen_set_wifi_imgbtn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_set, guider_ui.screen_set_del, &guider_ui.screen_set_wifi_del, setup_scr_screen_set, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 200, false, false);
        break;
    }
    default:
        break;
    }
}

static void screen_set_wifi_sw_WifiSwich_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_t *status_obj = lv_event_get_target(e);
        int status = lv_obj_has_state(status_obj, LV_STATE_CHECKED) ? 1 : 0;

        switch (status)
        {
        case (true):
        {
            wifi_switch_flag = 1;
            ESP_LOGI("wifi", "wifi_switch_flag = %d", wifi_switch_flag);
            write_wifi_sw_state(wifi_switch_flag); // wifi_flag write to nvs
            break;
        }
        case (false):
        {
            wifi_switch_flag = 0;
            ESP_LOGI("wifi", "wifi_switch_flag = %d", wifi_switch_flag);
            write_wifi_sw_state(wifi_switch_flag); // wifi_flag write to nvs
            break;
        }
        default:
            break;
        }
        break;
    }
    default:
        break;
    }
}

static void screen_set_wifi_imgbtn_SmartConfig_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_led_set_color(guider_ui.screen_set_wifi_led_SmartConfig, lv_color_hex(0xff0000));
        SmartConfig_flag = 1; // 开启smartconfig
        break;
    }
    default:
        break;
    }
}

static void screen_set_wifi_imgbtn_FindWifi_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        search_wifi_flag = 1; // 设置WiFi搜索标志位为1，开始搜索
        // 添加回调函数在LVGL列表中显示WiFi名称

        lv_led_set_color(guider_ui.screen_set_wifi_led_FindWifi, lv_color_hex(0xff0000));
        break;
    }
    default:
        break;
    }
}

static void screen_set_wifi_btn_wifiPassWord_OK_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_add_flag(guider_ui.screen_set_wifi_cont_WifiiPassWord, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    default:
        break;
    }
}

static void screen_set_wifi_btn_wifiPassWord_ESC_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_add_flag(guider_ui.screen_set_wifi_cont_WifiiPassWord, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    default:
        break;
    }
}

void events_init_screen_set_wifi(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_home, screen_home_event_handler, LV_EVENT_ALL, ui); // 主屏幕初始化事件
    lv_obj_add_event_cb(ui->screen_set_wifi, screen_set_wifi_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_wifi_imgbtn_1, screen_set_wifi_imgbtn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_wifi_sw_WifiSwich, screen_set_wifi_sw_WifiSwich_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_wifi_imgbtn_SmartConfig, screen_set_wifi_imgbtn_SmartConfig_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_wifi_imgbtn_FindWifi, screen_set_wifi_imgbtn_FindWifi_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_wifi_btn_wifiPassWord_OK, screen_set_wifi_btn_wifiPassWord_OK_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_wifi_btn_wifiPassWord_ESC, screen_set_wifi_btn_wifiPassWord_ESC_event_handler, LV_EVENT_ALL, ui);
}

static void screen_set_time_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_SCREEN_LOADED:
    {
        lv_obj_add_flag(guider_ui.screen_set_time_cont_set_data, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(guider_ui.screen_set_time_cont_SetTime, LV_OBJ_FLAG_HIDDEN);
        if (SetDateTimeFlag == 0) // 
        {
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_date, 100, LV_PART_MAIN);
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_time, 100, LV_PART_MAIN);
            lv_obj_add_state(guider_ui.screen_set_time_sw_set_time, LV_STATE_CHECKED);
        }
        else
        {
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_date, 255, LV_PART_MAIN);
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_time, 255, LV_PART_MAIN);
            lv_obj_clear_state(guider_ui.screen_set_time_sw_set_time, LV_STATE_CHECKED);
        }

        break;
    }
    default:
        break;
    }
}

static void screen_set_time_imgbtn_back_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        ui_load_scr_animation(&guider_ui, &guider_ui.screen_set, guider_ui.screen_set_del, &guider_ui.screen_set_time_del, setup_scr_screen_set, LV_SCR_LOAD_ANIM_OVER_RIGHT, 200, 200, false, true);
        break;
    }
    default:
        break;
    }
}
/*设置自动获取时间的开关按钮回调事件*/
static void screen_set_time_sw_set_time_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_t *status_obj = lv_event_get_target(e);
        int status = lv_obj_has_state(status_obj, LV_STATE_CHECKED) ? 1 : 0;

        switch (status)
        {
        case (true):
        {
            lv_label_set_text(guider_ui.screen_set_time_label_14, "true");
            SetDateTimeFlag = 0;

            write_time_sw_state(SetDateTimeFlag); // 写入nvs
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_date, 100, LV_PART_MAIN);
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_time, 100, LV_PART_MAIN);

            if(ntp_set_success_flag == false)//如果没有设置成功，则重新设置网络时间
            {
                get_ntp_time();
            }
            break;
        }
        case (false):
        {
            SetDateTimeFlag = 1;                  // 设置wifi开关的标志
            write_time_sw_state(SetDateTimeFlag); // 写入nvs
            lv_label_set_text(guider_ui.screen_set_time_label_14, "false");
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_time, 255, LV_PART_MAIN);
            lv_obj_set_style_opa(guider_ui.screen_set_time_btn_date, 255, LV_PART_MAIN);

            ntp_set_success_flag = false;//重置网络对时标志位
            break;
        }
        default:
            break;
        }
        break;
    }
    default:
        break;
    }
}
/*点击设置时间按钮回调函数*/
static void screen_set_time_btn_date_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        if (SetDateTimeFlag == 1)
        {

            if (time_year > 2024)
            {
                lv_roller_set_selected(guider_ui.screen_set_time_roller_1, datetime.year - 2024, LV_ANIM_OFF); // 年
            }
            lv_roller_set_selected(guider_ui.screen_set_time_roller_4, datetime.month - 1, LV_ANIM_OFF);                                   // 月
            lv_roller_set_selected(guider_ui.screen_set_time_roller_5, datetime.day - 1, LV_ANIM_OFF);                                     // 日
           

            lv_obj_clear_flag(guider_ui.screen_set_time_cont_set_data, LV_OBJ_FLAG_HIDDEN);
        }
        break;
    }
    default:
        break;
    }
}
/*设置时间回调函数*/
static void screen_set_time_btn_time_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        if (SetDateTimeFlag == 1)
        {
            lv_roller_set_selected(guider_ui.screen_set_time_roller_8, datetime.hour, LV_ANIM_OFF);                    // 时
            lv_roller_set_selected(guider_ui.screen_set_time_roller_6, datetime.minute / 10, LV_ANIM_OFF);             // 分十位
            lv_roller_set_selected(guider_ui.screen_set_time_roller_7, datetime.minute % 10, LV_ANIM_OFF);             // 分个位
           
            lv_obj_clear_flag(guider_ui.screen_set_time_cont_SetTime, LV_OBJ_FLAG_HIDDEN);
        }
        break;
    }
    default:
        break;
    }
}

static void screen_set_time_btn_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_add_flag(guider_ui.screen_set_time_cont_set_data, LV_OBJ_FLAG_HIDDEN);

        break;
    }
    default:
        break;
    }
}

static void screen_set_time_btn_2_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        datetime_t Now_datetime = {0};
        // 设置Now_datetime的年份为2025
        Now_datetime.year = time_year;
        // 设置Now_datetime的月份为1（即1月）
        Now_datetime.month = time_month;
        // 设置Now_datetime的日期为9（即9号）
        Now_datetime.day = time_day;
        // 调用PCF85063_Set_All函数，将Now_datetime的值设置到PCF85063芯片中
        PCF85063_Set_Date(Now_datetime);
        lv_obj_add_flag(guider_ui.screen_set_time_cont_set_data, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    default:
        break;
    }
}

static void screen_set_time_btn_SetTimeEsc_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {
        lv_obj_add_flag(guider_ui.screen_set_time_cont_SetTime, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    default:
        break;
    }
}

static void screen_set_time_btn_SetTimeOk_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_CLICKED:
    {

        printf("do set time"); /**/
        datetime_t Now_datetime = {0};
        // 设置Now_datetime的小时为9（即9点）
        Now_datetime.hour = time_hour;
        // 设置Now_datetime的分钟为50（即50分）
        Now_datetime.minute = (time_min_10 * 10 + time_min_01);
        // 调用PCF85063_Set_All函数，将Now_datetime的值设置到PCF85063芯片中
        PCF85063_Set_Time(Now_datetime);
        lv_obj_add_flag(guider_ui.screen_set_time_cont_SetTime, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    default:
        break;
    }
}
static void screen_set_time_roller_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e); // 获取触发事件的roller对象

    switch (code)
    {
    case LV_EVENT_VALUE_CHANGED:
    {
        if (obj == guider_ui.screen_set_time_roller_1)
        {
            char buf[32];
            lv_roller_get_selected_str(obj, buf, sizeof(buf));
            time_year = (uint16_t)atoi(buf);
        }
        else if (obj == guider_ui.screen_set_time_roller_4)
        {
            char buf[32];
            lv_roller_get_selected_str(obj, buf, sizeof(buf));
            time_month = (uint8_t)atoi(buf);
        }
        else if (obj == guider_ui.screen_set_time_roller_5)
        {
            char buf[32];
            lv_roller_get_selected_str(obj, buf, sizeof(buf));
            time_day = (uint8_t)atoi(buf);
        }
        else if (obj == guider_ui.screen_set_time_roller_8)
        {
            char buf[32];
            lv_roller_get_selected_str(obj, buf, sizeof(buf));
            time_hour = (uint8_t)atoi(buf);
        }
        else if (obj == guider_ui.screen_set_time_roller_6)
        {
            char buf[32];
            lv_roller_get_selected_str(obj, buf, sizeof(buf));
            time_min_10 = (uint8_t)atoi(buf);
        }
        else if (obj == guider_ui.screen_set_time_roller_7)
        {
            char buf[32];
            lv_roller_get_selected_str(obj, buf, sizeof(buf));
            time_min_01 = (uint8_t)atoi(buf);
        }

        break;
    }
    default:
        break;
    }
}
void events_init_screen_set_time(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->screen_set_time, screen_set_time_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_imgbtn_back, screen_set_time_imgbtn_back_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_sw_set_time, screen_set_time_sw_set_time_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_btn_date, screen_set_time_btn_date_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_btn_time, screen_set_time_btn_time_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_btn_1, screen_set_time_btn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_btn_2, screen_set_time_btn_2_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_btn_SetTimeEsc, screen_set_time_btn_SetTimeEsc_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->screen_set_time_btn_SetTimeOk, screen_set_time_btn_SetTimeOk_event_handler, LV_EVENT_ALL, ui);
    /**********************************************************/
    lv_obj_add_event_cb(ui->screen_set_time_roller_1, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui); // 年
    lv_obj_add_event_cb(ui->screen_set_time_roller_4, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui); // 月
    lv_obj_add_event_cb(ui->screen_set_time_roller_5, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui); // 日
    lv_obj_add_event_cb(ui->screen_set_time_roller_8, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui); // 时
    lv_obj_add_event_cb(ui->screen_set_time_roller_6, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui); // 分钟十位
    lv_obj_add_event_cb(ui->screen_set_time_roller_7, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui); // 分钟个位
}

void events_init(lv_ui *ui)
{
    //  lv_obj_add_event_cb(ui->screen_set_time_roller_1, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui);//年
    // lv_obj_add_event_cb(ui->screen_set_time_roller_4, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui);//月
    // lv_obj_add_event_cb(ui->screen_set_time_roller_5, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui);//日
    // lv_obj_add_event_cb(ui->screen_set_time_roller_8, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui);//时
    // lv_obj_add_event_cb(ui->screen_set_time_roller_6, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui);//分钟十位
    // lv_obj_add_event_cb(ui->screen_set_time_roller_7, screen_set_time_roller_1_event_handler, LV_EVENT_ALL, ui);//分钟个位
}
